package training.minggu5.maven.service.impl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import training.minggu5.maven.domain.PenjualanHeader;
import training.minggu5.maven.service.PenjualanHeaderService;

@Service("penjualanHeaderService")
@Transactional(readOnly=true)
public class PenjualanHeaderServiceImpl implements PenjualanHeaderService {

    @Autowired
    private SessionFactory sessionFactory;
    private String hqlFindAll = "from PenjualanHeader";
    private String hqlFindByNoFaktur = "from PenjualanHeader ph where ph.noFaktur = :nf";

    @Override
    @Transactional(readOnly=false)
    public void savePenjualanHeader(PenjualanHeader penjualan_header) {
        sessionFactory.getCurrentSession().saveOrUpdate(penjualan_header);
    }

    @Override
    @Transactional(readOnly=false)
    public void deletePenjualanHeader(PenjualanHeader penjualan_header) {
        sessionFactory.getCurrentSession().delete(penjualan_header);
    }

    @Override
    public List<PenjualanHeader> findAllPenjualanHeader() {
        return sessionFactory.getCurrentSession().createQuery(hqlFindAll).list();
    }

    @Override
    public PenjualanHeader findPenjualanHeaderByNoFaktur(String noFaktur) {
        return (PenjualanHeader) sessionFactory.getCurrentSession().createQuery(hqlFindByNoFaktur)
                .setParameter("nf", noFaktur).uniqueResult();
    }

}
